package loveATM.atm.Panel;

import jdk.nashorn.internal.scripts.JO;
import loveATM.User;
import loveATM.atm.ATM;
import loveATM.atm.FileUtil;
import loveATM.atm.Font.MyFont;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 改密界面
 */
public class AgainPwdPanel extends JPanel implements ActionListener {
    private ATM atm;
    MyFont myFont = new MyFont();
    private JLabel newPwdLabel = new JLabel("请输入新密码:");
    private JPasswordField newPwdTxtField = new JPasswordField();
    private JLabel newAgainPwdLabel = new JLabel("请再次输入新密码:");
    private JPasswordField newAgainPwdTxtField = new JPasswordField();

    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    public AgainPwdPanel(ATM atm){
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        newPwdLabel.setBounds(205,220,200,50);
        newPwdLabel.setFont(myFont.font);
        this.add(newPwdLabel);

        newPwdTxtField.setBounds(400,220,250,50);
        newPwdTxtField.setFont(myFont.font);
        this.add(newPwdTxtField);

        newAgainPwdLabel.setBounds(180,270,240,50);
        newAgainPwdLabel.setFont(myFont.font);
        this.add(newAgainPwdLabel);

        newAgainPwdTxtField.setBounds(400,270,250,50);
        newAgainPwdTxtField.setFont(myFont.font);
        this.add(newAgainPwdTxtField);




        sureBtn.setBounds(1024-190,300,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        offBtn.setBounds(1024-190,450,140,50);
        offBtn.addActionListener(this);
        this.add(offBtn);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atm.getUsers();
        int  index = atm.index;
        String newPwd = new String(newPwdTxtField.getPassword());
        String newAgainPwd = new String(newAgainPwdTxtField.getPassword());
        if (e.getSource() == sureBtn){
            if (newPwd.equals(newAgainPwd)){
                users.get(index).setPwd(newPwd);
                FileUtil.writeObject(users,atm.FILE_PATH);
                atm.showPanel("login");
            }else {
                JOptionPane.showMessageDialog(this,"两次密码不正确 ");
                atm.showPanel("again");
            }
            newAgainPwdTxtField.setText("");
            newPwdTxtField.setText("");

        }else if (e.getSource() == offBtn){
            newAgainPwdTxtField.setText("");
            newPwdTxtField.setText("");
            atm.showPanel("menu");
        }

    }
}